<template>
  <div class="login-contation">
    <!-- 导航栏组件 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" class="nav-cross" @click="$router.back()"></van-icon>
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" class="login-form" ref="loginformRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 3"
            format="ss s"
            v-if="isCountDoneShow"
            @finish='isCountDoneShow=false'
          />
          <van-button
            v-else
            class="fs-btn"
            round
            size="small"
            type="default"
            @click="onSendSms"
            native-type="button"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="tj">
        <van-button class="tj-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>

    <!--  -->
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isCountDoneShow: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0 // 默认只显示两秒。0一致显示
      })
      const user = this.user
      try {
        const { data } = await login(user)
        console.log(data.data)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          // console.log('手机号或验证码错误')
          this.$toast.fail('手机号或验证码错误')
        } else {
          // console.log('登录失败，请稍后重试')
          this.$toast.fail('登录失败，请稍后重试')
        }
      }
    },
    // 点击获取验证码
    async onSendSms () {
      // console.log('ok')
      try {
        await this.$refs.loginformRef.validate('mobile')
      } catch (error) {
        return console.log('验证失败', error)
      }
      this.isCountDoneShow = true
      try {
        await sendSms(this.user.mobile)
      } catch (error) {
        this.isCountDoneShow = false
        if (error.response.status === 429) {
          this.$toast.fail('发送频繁了，请稍后再试')
        } else {
          this.$toast.fail('登录失败请稍后再试')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-contation {
  .login-form {
    .iconfont {
      font-size: 37px;
    }
    .fs-btn {
      width: 152px;
      height: 46px;
      line-height: 46px;
      background-color: #ededed;
      .van-button__text {
        font-size: 22px;
      }
    }
  }
  .tj {
    margin: 53px 28px;
    .tj-btn {
      font-size: 30px;
      background-color: #6db4fb;
      border-radius: 10px;
    }
  }
  .nav-cross{
    color: #fff;
  }
}
</style>
